package com.topscore.uke.mvp.model;

import com.topscore.uke.entities.MyPerformanceData;
import com.topscore.uke.entities.MyPerformanceSummaryData;
import com.topscore.uke.entities.SalesRankingItem;
import com.topscore.uke.mvp.contractors.GetMyPerformanceContractor;
import com.topscore.uke.network.BaseResponse;
import com.topscore.uke.network.retrofit.HandleErrorSubscriber;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rx.Observable;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.observers.TestObserver;
import rx.schedulers.Schedulers;

/**
 * Created by 71468 on 2017/10/30.
 */

public class GetMyPerformanceModel implements GetMyPerformanceContractor, GetMyPerformanceContractor.Model {

    private CallBack mCallBack;

    public GetMyPerformanceModel(CallBack callBack) {
        this.mCallBack = callBack;
    }

    @Override
    public void getPerformanceData(String userId) {
        mService.getMyPerformanceChartData(userId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse<MyPerformanceSummaryData>>(mCallBack) {
                    @Override
                    public void onSuccess(BaseResponse<MyPerformanceSummaryData> response) {
                        mCallBack.onGetSummarySuccess(response);
                    }
                });
    }

    @Override
    public void getSalesRankList(String userId, String sortName) {
        Map<String, Object> request = new HashMap<>();
        mService.getSalesRankingListTop15(request)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse<List<SalesRankingItem>>>(mCallBack) {
                    @Override
                    public void onSuccess(BaseResponse<List<SalesRankingItem>> response) {
                        mCallBack.onGetRankListDataSuccess(response);
                    }
                });
//        mService.getPersonalSalesRank(userId, sortName)
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .unsubscribeOn(Schedulers.io())
//                .subscribe(new HandleErrorSubscriber<BaseResponse<List<SalesRankingItem>>>(mCallBack) {
//                    @Override
//                    public void onSuccess(BaseResponse<List<SalesRankingItem>> response) {
//                        mCallBack.onGetRankListDataSuccess(response);
//                    }
//                });
    }

    @Override
    public void getData(String userId) {
        Map<String, Object> request = new HashMap<>();
        Observable observable = Observable.merge(mService.getMyPerformanceChartData(userId), mService.getSalesRankingListTop15(request));
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse<?>>(mCallBack) {
                    @Override
                    public void onSuccess(BaseResponse<?> response) {
                        if (response.getData() instanceof MyPerformanceSummaryData) {
                            mCallBack.onGetSummarySuccess((BaseResponse<MyPerformanceSummaryData>) response);
                        }else if (response.getData() instanceof List){
                            if (!((List)response.getData()).isEmpty()){
                                if (((List) response.getData()).get(0) instanceof SalesRankingItem){
                                    mCallBack.onGetRankListDataSuccess((BaseResponse<List<SalesRankingItem>>) response);
                                }
                            }
                        }
                    }
                });
    }
}